/*
  // Temp controller:
  err = expect_temp - temp;
  control = pid_controller(err);
 */

// a struct pass PID params
struct pid_param {
  float k_p, k_i, k_d;
  float sum;
  float pre_err;
};

// for easy program writing
typedef struct pid_param pid_param;

// integrate
float integrate(float err, pid_param *pid) {
  pid->sum += err;
  return pid->sum;
}

// give the control signal by input err
float pid_controller(float err, pid_param *pid) {
  float control;
  control = pid->k_p * err +
    pid->k_i * integrate(err, pid) +
    pid->k_d * (err - pid->pre_err);

  pid->pre_err = err;
  return control;
}
